package lib.scrren;

import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;

import javax.swing.JComponent;

public abstract class DisplayAble {
	private Screen parent = Screen.getInstance();

	public abstract String draw(Graphics2D g);

	private KeyListener KeyAdapter = new KeyAdapter() {
		public void keyPressed(KeyEvent e) {
			switch (e.getKeyCode()) {
			case KeyEvent.VK_ESCAPE:
				exit();
			}
		}
	};

	protected void startDrawing() {
		parent.setDisplay(this);
		parent.frame.addKeyListener(KeyAdapter);
	}

	public void removeKeyAdapter(KeyListener k) {
		parent.frame.removeKeyListener(k);
	}

	public void exit() {
		parent.frame.removeKeyListener(getKeyAdapter());
		parent.setDisplay(null);
		parent.exit();
	}

	public void setKeyAdapter(KeyListener keyPressed) {
		parent.frame.removeKeyListener(KeyAdapter);
		KeyAdapter = keyPressed;
		parent.frame.addKeyListener(KeyAdapter);
	}

	public KeyListener getKeyAdapter() {
		return KeyAdapter;
	}

	public void addComponent(JComponent c) {
		parent.content.add(c);
		c.setOpaque(false);
		parent.frame.addMouseListener(new MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				parent.frame.requestFocus();
			}
		});
	}

	public void removeComponent(JComponent c) {
		parent.content.remove(c);
	}

	public void drawComponents(boolean d) {
		parent.components = d;
	}

}
